<?php

namespace backend\controllers;

use common\models\UserInfo;
use frontend\models\UserDecorate;
use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\ForbiddenHttpException;
use yii\grid\GridView;
use yii\helpers\Html;
use yii\base\Object;
use backend\models\UserDecorateSearch;

use backend\models\AdminInfo;
/**
 * 装修 控制器
 * @author suiyue
 *
 */
class UserDecorateController extends Controller
{
    /**
     * (non-PHPdoc)行为
     * @see \yii\base\Component::behaviors()
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    
    /**
     * (non-PHPdoc)前置函数
     * @see \yii\web\Controller::beforeAction()
     */
    public function beforeAction($action)
    {
    	//如果未登录，则直接跳转
    	if(Yii::$app->user->isGuest){
    		return $this->redirect(['/site/login']);
    	}
    	$action = Yii::$app->controller->action->id;
    	$controller = Yii::$app->controller->id;
    	if(\Yii::$app->user->can('user-decorate-manage') ){
    		if($action == 'create' && AdminInfo::check_create('create') == true){
				return true;
			}
    		if($this->_access($action) == true){
    			return true;
    		}
    	}else{
    		throw new ForbiddenHttpException(Yii::t('app','Sorry , You don\'t have permission'));
    	}
    }
	
	//允许访问
    function _access($action){
    	$action_list = array(
    		'index',
    	    'view',
    	    'create',
    	    'update'
    	);
    	if(in_array($action, $action_list)){
    		return true;
    	}
	}
    /**
     * 列表
     */
    public function actionIndex()
    {
        $searchModel = new UserDecorateSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * 创建
     */
    public function actionCreate(){
        $this->layout = 'main_small';
        $model = new UserDecorate();
        if ($model->load(Yii::$app->request->post())&&$model->validate()) {
            $userInfo = UserInfo::getUserByTel($model->phone);
            if($userInfo === null){
                throw new ForbiddenHttpException(Yii::t('app','Sorry , User is not exist'));
            }
            $model->user_id = $userInfo->id;
            $model = $model->createByAdmin();

            return $this->redirect(['user-decorate/view', 'id' => $model->id]);
            
        } else {
            return $this->render('form', [
                'formModel' => $model,
            ]);
        }
    }
    
    
    /**
     * 修改
     */
    public function actionUpdate(){
        $this->layout = 'main_small';
        $id=Yii::$app->request->get('id');
        $model = UserDecorate::getDecorate($id,true);
        if($model === null){
            throw new ForbiddenHttpException(Yii::t('app','Sorry , Data is not exist'));
        }
        $model->edit = 1;

        if ($model->load(Yii::$app->request->post())/*&& var_dump($model->validate())*/) {
            $model->updateByAdmin($id);
            return $this->redirect(['user-decorate/view', 'id' => $model->id]);
            
        } else {
            return $this->render('form', [
                'formModel' => $model,
            ]);
        }
    }
    /**
     * 查看单条数据
     */
    public function actionView(){
        $this->layout = 'main_small';
        $id=Yii::$app->request->get('id');
        $model = UserDecorate::getDecorate($id);
        $model = UserDecorate::viewData($model);
        return $this->render('view', [
            'model' => $model
        ]);
    }
    
    /**
     * 查找单个
     * @param   $id
     * @throws NotFoundHttpException
     */
    protected function findModel($id)
    {
        if (($model = AfterService::findOne($id)) !== null) {
            $model = AfterService::find()->where('id='.$id)->one();
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    /**
     * (non-PHPdoc)
     * @see \yii\base\Controller::actions()
     * by suiyue
     */
    public function actions(){
        return [
            'upload'=>[
                'class'=>'backend\widgets\ueditor\UEditorAction',
                'config'=>[
                    //  /gjj/backend/web/images/
                    'imageUrlPrefix'=>"https://".$_SERVER['SERVER_NAME']."/gjj/backend/web/images/",//图片访问路径前缀
                    'imagePathFormat'=>'{rand:2}/{time}{rand:6}',
                    'imageRoot'=>\Yii::getAlias("images")
                ]
            ]
        ];
        
    }
}
